<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-9-1
 * Time: 下午9:17
 * To change this template use File | Settings | File Templates.
 */
class login extends MY_Controller
{
    public function index()
    {
        $redirect_url = $this->input->get_post('redirect_url');

        $vData = array(
            'title' => '用户登陆 电影1010',
            'redirect_url' => $redirect_url,
            'keywords' => '电影1010 登陆',
            'description' => '电影1010 全网最全资源库',
        );

        $this->load->view('login', $vData);
    }

    public function submit()
    {
        $this->load->helper('validation');

        $userName = $this->input->get_post('username');
        $passWord = $this->input->get_post('password');
        $redirectUrl = $this->input->get_post('redirect_url');
        $redirectUrl = is_url($redirectUrl) ? $redirectUrl : config_item('i_base_url'); //跳转地址
        $source = intval($this->input->get_post('source'));
        $ip = $this->input->ip_address();

        $response = array('error' => '0', 'msg' => '用户登陆成功', 'code' => 'user_login_success');
        $response['redirect_url'] = $redirectUrl;

        do {
            if ($this->isLogin()) {
                break;
            }

            $this->load->helper('validation');
            $this->load->helper('cookie');

            if (empty ($userName) || empty ($passWord)) {
                $response = array('error' => '1001', 'msg' => '用户名或密码为空', 'code' => 'username_and_password_empty');
                break;
            }

            if (!length_limit($userName, 6, 32)) {
                $response = array('error' => '1002', 'msg' => '用户名格式错误', 'code' => 'user_format_error');
                break;
            }

            if (!length_limit($passWord, 6, 32)) {
                $response = array('error' => '1003', 'msg' => '密码格式错误', 'code' => 'password_format_error');
                break;
            }

            $this->load->model('user');
            $uInfo = $this->user->userLogin($userName, $passWord);

            if ($uInfo == 1) {
                $response = array('error' => '1004', 'msg' => '用户不存在', 'code' => 'user_not_exist');
                break;
            }

            if ($uInfo == 2) {
                $response = array('error' => '1005', 'msg' => '密码错误', 'code' => 'password_error');
                break;
            }

            //设置用户登陆状态
            set_cookie('auth', authcode("{$uInfo['id']}\t{$uInfo['username']}\t{$uInfo['password']}", 'ENCODE'), (86400 * 360));
            $this->user->record_login_log($uInfo['id'], $ip, $source);
        } while (false);

        self::json_output($response);
    }

    /**
     * 退出登陆
     */
    public function out()
    {
        $this->load->helper('url');
        $this->load->helper('cookie');
        delete_cookie('auth');

        unset ($this->uInfo);

        redirect(config_item('i_base_url'));
    }

    /**
     * 获取用户信息
     */
    public function getUserInfo ()
    {
        $response = array('error' => '1009', 'msg' => '用户未登陆', 'code' => 'User_not_logged');

        if ($this->isLogin()) {
            $response = array('error' => '0', 'msg' => '登陆成功', 'code' => 'login_success');
            $response['user_info'] = $this->uInfo;
        }

        self::json_output($response);
    }
}
